package com.ksd.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ksd.common.annotation.Log;
import com.ksd.common.core.controller.BaseController;
import com.ksd.common.core.domain.AjaxResult;
import com.ksd.common.core.page.TableDataInfo;
import com.ksd.common.enums.BusinessType;
import com.ksd.common.utils.poi.ExcelUtil;
import com.ksd.quartz.domain.SysJob;
import com.ksd.quartz.service.ISysJobService;
import org.quartz.SchedulerException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ksd.system.domain.SysContentSynch;
import com.ksd.system.service.ISysContentSynchService;

/**
 * 内容同步管理Controller
 * 
 * @author y.wu
 * @date 2025-09-08
 */
@RestController
@RequestMapping("/system/contentSynch")
public class SysContentSynchController extends BaseController
{
    @Autowired
    private ISysContentSynchService sysContentSynchService;
    @Autowired
    private ISysJobService sysJobService;

    /**
     * 查询内容同步管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:contentSynch:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysContentSynch sysContentSynch)
    {
        startPage();
//        List<SysContentSynch> list = sysContentSynchService.selectSysContentSynchList(sysContentSynch);
        SysContentSynch sysContentSynchTemp = sysContentSynchService.selectSysContentSynchById(SysContentSynch.cloudSynchConfig);
        if (sysContentSynchTemp.getIntValue().equals("0")){
            sysContentSynchTemp.setIntValue("开启");
        }else if (sysContentSynchTemp.getIntValue().equals("1")){
            sysContentSynchTemp.setIntValue("关闭");
        }
        List<SysContentSynch> list = new ArrayList<SysContentSynch>();
        list.add(sysContentSynchTemp);
        return getDataTable(list);
    }

    @GetMapping("/listVersion")
    public TableDataInfo listVersion(SysContentSynch sysContentSynch)
    {
        startPage();
        SysContentSynch apkVersion = sysContentSynchService.selectSysContentSynchById(SysContentSynch.synch_apk_id);
        SysContentSynch jarVersion = sysContentSynchService.selectSysContentSynchById(SysContentSynch.synch_jar_id);
        SysContentSynch vueVersion = sysContentSynchService.selectSysContentSynchById(SysContentSynch.synch_ui_id);

        if (apkVersion.getIntValue().equals("0")){
            apkVersion.setIntValue("开启");
        }else if (apkVersion.getIntValue().equals("1")){
            apkVersion.setIntValue("关闭");
        }

        if (jarVersion.getIntValue().equals("0")){
            jarVersion.setIntValue("开启");
        }else if (jarVersion.getIntValue().equals("1")){
            jarVersion.setIntValue("关闭");
        }

        if (vueVersion.getIntValue().equals("0")){
            vueVersion.setIntValue("开启");
        }else if (vueVersion.getIntValue().equals("1")){
            vueVersion.setIntValue("关闭");
        }

        List<SysContentSynch> list = new ArrayList<SysContentSynch>();
        list.add(apkVersion);
        list.add(jarVersion);
        list.add(vueVersion);
        return getDataTable(list);
    }
    /**
     * 导出内容同步管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:contentSynch:export')")
    @Log(title = "内容同步管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysContentSynch sysContentSynch)
    {
        List<SysContentSynch> list = sysContentSynchService.selectSysContentSynchList(sysContentSynch);
        ExcelUtil<SysContentSynch> util = new ExcelUtil<SysContentSynch>(SysContentSynch.class);
        util.exportExcel(response, list, "内容同步管理数据");
    }

    /**
     * 获取内容同步管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:contentSynch:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysContentSynchService.selectSysContentSynchById(id));
    }

    /**
     * 新增内容同步管理
     */
    @PreAuthorize("@ss.hasPermi('system:contentSynch:add')")
    @Log(title = "内容同步管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysContentSynch sysContentSynch)
    {
        return toAjax(sysContentSynchService.insertSysContentSynch(sysContentSynch));
    }

    /**
     * 修改内容同步管理
     */
    @PreAuthorize("@ss.hasPermi('system:contentSynch:edit')")
    @Log(title = "内容同步管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysContentSynch sysContentSynch) throws SchedulerException {

        SysJob sysJob = sysJobService.selectJobById(sysContentSynch.getId());
        sysJob.setStatus(sysContentSynch.getIntValue());
        sysJobService.changeStatus(sysJob);
        String intValue = sysContentSynch.getIntValue();
        if (intValue.equals("0")){
            //开启,立即执行
            logger.info("状态开启,立即执行一次");
            sysJobService.run(sysJob);
        }else{
            logger.info("状态关闭");
        }
        return toAjax(sysContentSynchService.updateSysContentSynch(sysContentSynch));
    }

    /**
     * 删除内容同步管理
     */
    @PreAuthorize("@ss.hasPermi('system:contentSynch:remove')")
    @Log(title = "内容同步管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysContentSynchService.deleteSysContentSynchByIds(ids));
    }
}
